package org.jeecg.modules.outbound.service.impl;

import org.jeecg.modules.inventory.entity.BussInventoryInoutDetails;
import org.jeecg.modules.inventory.mapper.BussInventoryInoutDetailsMapper;
import org.jeecg.modules.outbound.entity.*;
import org.jeecg.modules.outbound.mapper.BussProductOutboundOrderInformationMapper;
import org.jeecg.modules.outbound.mapper.BussProductOutboundOrderDetailMapper;
import org.jeecg.modules.outbound.mapper.BussProductOutboundOrderManagementMapper;
import org.jeecg.modules.outbound.service.IBussProductOutboundOrderManagementService;
import org.jeecg.modules.inventory.entity.BussInventory;
import org.jeecg.modules.inventory.entity.BussInventoryDetails;
import org.jeecg.modules.inventory.mapper.BussInventoryDetailsMapper;
import org.jeecg.modules.inventory.mapper.BussInventoryMapper;
import org.jeecg.modules.outbound.entity.BussProductOutboundOrderDetail;
import org.jeecg.modules.outbound.entity.BussProductOutboundOrderInformation;
import org.jeecg.modules.outbound.vo.BussOutboundOrderDetailVo;
import org.jeecg.modules.outbound.vo.BussProductOutboundOrderDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 成品出库单管理
 * @Author: jeecg-boot
 * @Date:   2024-07-17
 * @Version: V1.0
 */
@Service
public class BussProductOutboundOrderManagementServiceImpl extends ServiceImpl<BussProductOutboundOrderManagementMapper, BussProductOutboundOrderManagement> implements IBussProductOutboundOrderManagementService {

	@Autowired
	private BussProductOutboundOrderManagementMapper bussProductOutboundOrderManagementMapper;
	@Autowired
	private BussProductOutboundOrderInformationMapper bussProductOutboundOrderInformationMapper;
	@Autowired
	private BussProductOutboundOrderDetailMapper bussProductOutboundOrderDetailMapper;
	@Autowired
	private BussInventoryMapper bussInventoryMapper;
	@Autowired
	private BussInventoryDetailsMapper bussInventoryDetailsMapper;
	@Autowired
	private BussInventoryInoutDetailsMapper bussInventoryInoutDetailsMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BussProductOutboundOrderManagement bussProductOutboundOrderManagement, List<BussProductOutboundOrderInformation> bussOutboundOrderInformationList, List<BussProductOutboundOrderDetailVo> outboundOrderDetailList) {
		bussProductOutboundOrderManagement.setStatus("1");//默认已审批
		bussProductOutboundOrderManagementMapper.insert(bussProductOutboundOrderManagement);
		if(bussOutboundOrderInformationList!=null && bussOutboundOrderInformationList.size()>0) {
			for(BussProductOutboundOrderInformation entity:bussOutboundOrderInformationList) {
				entity.setId(null);
				//外键设置
				entity.setOutboundId(bussProductOutboundOrderManagement.getId());
				bussProductOutboundOrderInformationMapper.insert(entity);
			}
		}
		if(outboundOrderDetailList!=null && outboundOrderDetailList.size()>0) {
			for(BussProductOutboundOrderDetailVo entity:outboundOrderDetailList) {
				BussProductOutboundOrderDetail bussOutboundOrderDetail = new BussProductOutboundOrderDetail();
				BeanUtils.copyProperties(entity, bussOutboundOrderDetail);
				entity.setId(null);
				//外键设置
				entity.setInformationId(bussProductOutboundOrderManagement.getId());
				if (entity.getQuantity() != null && entity.getQuantity().compareTo(BigDecimal.ZERO) != 0) {
					bussProductOutboundOrderDetailMapper.insert(entity);
				}
//				if (entity.getChildren()!=null && entity.getChildren().size()>0){
//					for(BussProductOutboundOrderDetailVo temp:entity.getChildren()) {
//						BussProductOutboundOrderDetail detail = new BussProductOutboundOrderDetail();
//						BeanUtils.copyProperties(temp, detail);
//						detail.setId(null);
//						detail.setInformationId(bussProductOutboundOrderManagement.getId());
//						detail.setPid(entity.getId());
//						detail.setHasChild("0");
//						if(detail.getQuantity() != null && detail.getQuantity().compareTo(BigDecimal.ZERO) != 0) {
//							bussProductOutboundOrderDetailMapper.insert(detail);
//						}
//					}
//				}
			}
		}
		saveInventory(bussProductOutboundOrderManagement,bussOutboundOrderInformationList,outboundOrderDetailList);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BussProductOutboundOrderManagement bussProductOutboundOrderManagement,List<BussProductOutboundOrderInformation> bussOutboundOrderInformationList,List<BussProductOutboundOrderDetailVo> outboundOrderDetailList) {
		//判断出库明细对应的库存存不存在，不存在提示
		if(outboundOrderDetailList!=null && outboundOrderDetailList.size()>0) {
			for(BussProductOutboundOrderDetailVo detailVo:outboundOrderDetailList) {
				BussInventoryDetails details = bussInventoryDetailsMapper.selectById(detailVo.getInventoryId());
				if (details == null){
					throw new RuntimeException("出库单对应的库存明细发生变化，请将出库单信息内容删除重新选择出库的物料！");
				}
			}
		}
		bussProductOutboundOrderManagement.setStatus("1");//默认已审批
		bussProductOutboundOrderManagementMapper.updateById(bussProductOutboundOrderManagement);

		//1.先删除子表数据
		//删除前恢复库存数量，删除出库记录
//		quantityRefund(bussProductOutboundOrderManagement.getId());
		bussProductOutboundOrderInformationMapper.deleteByMainId(bussProductOutboundOrderManagement.getId());
		bussProductOutboundOrderDetailMapper.deleteByMainId(bussProductOutboundOrderManagement.getId());

		//2.子表数据重新插入
		if(bussOutboundOrderInformationList!=null && bussOutboundOrderInformationList.size()>0) {
			for(BussProductOutboundOrderInformation entity:bussOutboundOrderInformationList) {
				entity.setId(null);
				//外键设置
				entity.setOutboundId(bussProductOutboundOrderManagement.getId());
				bussProductOutboundOrderInformationMapper.insert(entity);
			}
		}
		if(outboundOrderDetailList!=null && outboundOrderDetailList.size()>0) {
			for(BussProductOutboundOrderDetailVo entity:outboundOrderDetailList) {
				BussProductOutboundOrderDetail bussOutboundOrderDetail = new BussProductOutboundOrderDetail();
				BeanUtils.copyProperties(entity, bussOutboundOrderDetail);
				entity.setId(null);
				//外键设置
				entity.setInformationId(bussProductOutboundOrderManagement.getId());
				if (entity.getQuantity() != null && entity.getQuantity().compareTo(BigDecimal.ZERO) != 0) {
					bussProductOutboundOrderDetailMapper.insert(entity);
				}
//				if (entity.getChildren()!=null && entity.getChildren().size()>0){
//					for(BussProductOutboundOrderDetailVo temp:entity.getChildren()) {
//						BussProductOutboundOrderDetail detail = new BussProductOutboundOrderDetail();
//						BeanUtils.copyProperties(temp, detail);
//						detail.setId(null);
//						detail.setInformationId(bussProductOutboundOrderManagement.getId());
//						detail.setPid(entity.getId());
//						detail.setHasChild("0");
//						bussProductOutboundOrderDetailMapper.insert(detail);
//					}
//				}
			}
		}
		saveInventory(bussProductOutboundOrderManagement,bussOutboundOrderInformationList,outboundOrderDetailList);

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		BussProductOutboundOrderManagement management = bussProductOutboundOrderManagementMapper.selectById(id);
		if(management.getStatus().equals(1)){
			throw  new RuntimeException("出库单已审批不可删除！");
		}
		bussProductOutboundOrderInformationMapper.deleteByMainId(id);
		bussProductOutboundOrderDetailMapper.deleteByMainId(id);
		bussProductOutboundOrderManagementMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			BussProductOutboundOrderManagement management = bussProductOutboundOrderManagementMapper.selectById(id);
			if(management.getStatus().equals(1)){
				throw  new RuntimeException("出库单已审批不可删除！");
			}
			bussProductOutboundOrderInformationMapper.deleteByMainId(id.toString());
			bussProductOutboundOrderDetailMapper.deleteByMainId(id.toString());
			bussProductOutboundOrderManagementMapper.deleteById(id);
		}
	}

	/**
	 *
	 *
	 *
	 * @param bussProductOutboundOrderManagement
	 * @param bussOutboundOrderInformationList
	 * @param outboundOrderDetailList
	 */
	public void saveInventory(BussProductOutboundOrderManagement bussProductOutboundOrderManagement,List<BussProductOutboundOrderInformation> bussOutboundOrderInformationList,List<BussProductOutboundOrderDetailVo> outboundOrderDetailList){

		for (BussProductOutboundOrderDetailVo detailVo:outboundOrderDetailList){
			if(detailVo.getQuantity() != null && detailVo.getQuantity().compareTo(BigDecimal.ZERO) != 0) {
				//库存明细出库数量，库存数量
				BussInventoryDetails details = bussInventoryDetailsMapper.selectById(detailVo.getInventoryId());
				details.setOutboundQuantity(details.getOutboundQuantity().add(detailVo.getQuantity()));
				details.setQuantity(details.getQuantity().subtract(detailVo.getQuantity()));
				bussInventoryDetailsMapper.updateById(details);
				//生成库存明细出库记录
				BussInventoryInoutDetails bussInventoryInoutDetails = new BussInventoryInoutDetails();
				bussInventoryInoutDetails.setOperator(bussProductOutboundOrderManagement.getCreateBy());
				bussInventoryInoutDetails.setOperationTime(bussProductOutboundOrderManagement.getCreateTime());
				bussInventoryInoutDetails.setBusiness("5");//业务默认出库
				bussInventoryInoutDetails.setBusinessType("9");//业务类型判断暂时赋值成品出库
				bussInventoryInoutDetails.setQuantity(detailVo.getQuantity());
				bussInventoryInoutDetails.setBusinessCode(bussProductOutboundOrderManagement.getCode());
				bussInventoryInoutDetails.setBusinessId(bussProductOutboundOrderManagement.getId());
				bussInventoryInoutDetails.setInventoryId(details.getInventoryId());
				bussInventoryInoutDetails.setDetailId(details.getId());
				bussInventoryInoutDetails.setRemark(detailVo.getRemark());
				bussInventoryInoutDetailsMapper.insert(bussInventoryInoutDetails);
			}
		}
	}
	@Override
	public void revokeBatch(Collection<? extends Serializable> idList) {
		for (Serializable id : idList) {
			BussProductOutboundOrderManagement management = bussProductOutboundOrderManagementMapper.selectById(id.toString());
			management.setStatus("0");
			bussProductOutboundOrderManagementMapper.updateById(management);
			quantityRefund(id.toString());
		}
	}

	public void quantityRefund(String id){
		BussProductOutboundOrderManagement management = bussProductOutboundOrderManagementMapper.selectById(id);
		//查询出库明细
		List<BussProductOutboundOrderDetail> details = bussProductOutboundOrderDetailMapper.selectByInfoId(id);
		for (BussProductOutboundOrderDetail detailVo : details){
			//将数量加回库存明细
			BussInventoryDetails inventory = bussInventoryDetailsMapper.selectById(detailVo.getInventoryId());
			inventory.setQuantity(inventory.getQuantity().add(detailVo.getQuantity()));
			//可用数量
			inventory.setOutboundQuantity(inventory.getOutboundQuantity().subtract(detailVo.getQuantity()));
			bussInventoryDetailsMapper.updateById(inventory);
		}


		//删除对应的库存出库明细
		bussInventoryInoutDetailsMapper.deleteByBusinessId(id,management.getCode());

	}
}
